<?php


class sfWidgetFormRange extends sfWidgetForm
{
  /**
   * Constructor.
   *
   * Available options:
   *
   *  * min:                Integer - min value
   *  * max:                Integer - max value
   *  * template:           The HTML template to use to render this widget
   *
   * @param array $options     An array of options
   * @param array $attributes  An array of default HTML attributes
   *
   * @see sfWidgetForm
   */
  protected function configure($options = array(), $attributes = array())
  {
    $this->addRequiredOption('minvalue');
    $this->addRequiredOption('maxvalue');
    $this->addOption('minbordervalue', 0);
    $this->addOption('maxbordervalue', 9999);
    $this->addOption('money', '€');
    $this->addOption('embed', false);
    $this->addOption('ajax', false);
    
    $this->addOption('template', <<<EOF
  <span id="range_rs"></span>
  <div id="slider-range"></div>
EOF
);




    
  
  }

  /**
   * Renders the widget.
   *
   * @param  string $name        The element name
   * @param  string $value       The value selected in this widget
   * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
   * @param  array  $errors      An array of errors for the field
   *
   * @return string An HTML tag string
   *
   * @see sfWidgetForm
   */
  public function render($name, $value = null, $attributes = array(), $errors = array())
  {
    if($this->getOption('embed')){
      // debug - transform embed $name
      $name = str_replace('[','_',$name);
      $name = str_replace(']','',$name);      
    }
    
  
    $hiddenfields = array();
    $hiddenfields['min'] =  new sfWidgetFormInputHidden(array('default' => $this->getOption('minvalue')));
    $hiddenfields['max'] =  new sfWidgetFormInputHidden(array('default' => $this->getOption('maxvalue')));
  
    $renderHiddenfields = '';
    foreach($hiddenfields as $key => $field){
      $renderHiddenfields .= $field->render($name.'-'.$key);
    }

    $js = <<<EOF
  $(document).ready(function(e){
    $( "#slider-range" ).slider({
      range: true,
      min: %minbordervalue%,
      max: %maxbordervalue%,
      values: [ %mindefaultvalue%, %maxdefaultvalue% ],
      slide: function( event, ui ) {
        $( "#range_rs" ).html( ui.values[ 0 ] + "%money% - " + ui.values[ 1 ] + "%money%" );
        $( "#%name%-min" ).val( ui.values[ 0 ] );
        $( "#%name%-max" ).val( ui.values[ 1 ] );
      },
      stop: function (event, ui ) {
        %stopfunction%
      }
    });
    $( "#range_rs" ).html( $( "#slider-range" ).slider( "values", 0 ) +
      "%money% - " + $( "#slider-range" ).slider( "values", 1 ) + "%money%" );
  });
EOF;
    $stopfunction = <<<EOF
$.ajax({ 
  type: 'POST',
  url: '%ajax_url%', 
  data: $('#minisearch').serialize(),
  beforeSubmit: $('#loaderMiniSearch').show(),
  success: function(output){
    $('#loaderMiniSearch').hide();
    $('#output1').html(output);
  }
});
EOF;


    sfProjectConfiguration::getActive()->loadHelpers(array('JavascriptBase','Tag'));
    $renderJavascript = javascript_tag(
      strtr($js,
            array('%stopfunction%' => ($this->getOption('ajax')?$stopfunction:'')
            )
      )
    );

  
    $output = '%hiddenfields%'. ' '.
              $this->getOption('template'). ' '.
              $renderJavascript;
    return strtr($output, array(
      '%hiddenfields%'    => $renderHiddenfields,
      '%name%'            => $name, 
      '%minbordervalue%'  => $this->getOption('minbordervalue'),
      '%maxbordervalue%'  => $this->getOption('maxbordervalue'),
      '%money%'           => $this->getOption('money'),
      '%mindefaultvalue%' => $this->getOption('minvalue'),
      '%maxdefaultvalue%' => $this->getOption('maxvalue'),
      '%ajax_url%'        => $this->getOption('ajax'),
    ));
  }

  
  public function getJavascripts()
  {
    return array('/js/jquery.ui.slider.js');
  }


}
